package cn.tony.game.super_tank.msg;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

import cn.tony.game.super_tank.Game;
import cn.tony.game.super_tank.Tank;

public class ZidanMessage extends Message {

    private int   type = 3;

    public String name;

    public int    id;

    public int    x;

    public int    y;

    public int    dir;

    public int    currentBlood;

    
    
    @Override
    public void send(Socket socket) {
        // dos
        try {
            DataOutputStream dos = new DataOutputStream(socket.getOutputStream());

            dos.writeInt(type);

            dos.writeUTF(name);
            dos.writeInt(id);
            dos.writeInt(x);
            dos.writeInt(y);
            dos.writeInt(dir);
            dos.writeInt(currentBlood);
            dos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void reslove(DataInputStream dis) {
        try {
            name = dis.readUTF();
            id = dis.readInt();
            x = dis.readInt();
            y = dis.readInt();
            dir = dis.readInt();
            currentBlood = dis.readInt();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void doSomething(Game game) {
        Tank tank = game.updateTank(id, name, x, y, dir, currentBlood);
        tank.trigger();
    }
    
    public ZidanMessage buildMsg(Game game) {
        ZidanMessage join = new ZidanMessage();
        join.dir = game.player.dir;
        join.x = game.player.x;
        join.y = game.player.y;
        join.id = game.player.id;
        join.currentBlood = game.player.gettBloodStrip();
        join.name = game.player.name.name;
        return join;
    }


}
